package com.chs.recoginition.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.chs.recoginition.core.utils.FileAndBaseUtils;
import com.chs.recoginition.core.utils.UploadUtil;
import com.chs.recoginition.service.ImageRecognitionService;
import com.chs.recoginition.vo.Result;
import com.chs.recoginition.vo.respVo.ImageRecognitionRespVo;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.Map;

/**
 * @Author CHS
 * @Date 2023/2/9 0:01
 * @Version 1.0
 **/
@RestController
@RequestMapping("/imageRecognition")
public class ImageRecognitionController {
    @Resource
    private ImageRecognitionService imageRecognitionService;

    @Value("${json.message}")
    private String message;
    @Value("${json.initApps}")
    private String initApps;

    /**
     * 获取百度云令牌
     *
     * @return 令牌
     */
    @PostMapping("/getToken")
    Result<String> getToken() {
        return Result.success(imageRecognitionService.getToken());
    }

    /**
     * 通用物品和场景识别
     *
     * @return
     */
    @PostMapping("/getAdvancedGeneral")
    Result<ImageRecognitionRespVo> getAdvancedGeneral(@RequestBody String image64) throws IOException {
        return Result.success(imageRecognitionService.getAdvancedGeneral(image64));
    }

    /**
     * 动物识别
     *
     * @param response
     * @param file
     * @return
     * @throws IOException
     */
    @PostMapping("/getAnimal")
    Result<ImageRecognitionRespVo> getAnimal(@RequestBody String image64) throws IOException {
        return Result.success(imageRecognitionService.getAnimal(image64));
    }

    /**
     * 植物识别
     *
     * @param response
     * @param file
     * @return
     * @throws IOException
     */
    @PostMapping("/getPlant")
    Result<ImageRecognitionRespVo> getPlant(@RequestBody String image64) throws IOException {
        return Result.success(imageRecognitionService.getPlant(image64));
    }

    /**
     * logo识别
     *
     * @param response
     * @param file
     * @return
     * @throws IOException
     */
    @PostMapping("/getLogo")
    Result<ImageRecognitionRespVo> getLogo(@RequestBody String image64) throws IOException {
        return Result.success(imageRecognitionService.getLogo(image64));
    }

    /**
     * 果蔬识别
     *
     * @param response
     * @param file
     * @return
     * @throws IOException
     */
    @PostMapping("/getIngredient")
    Result<ImageRecognitionRespVo> getIngredient(@RequestBody String image64) throws IOException {
        return Result.success(imageRecognitionService.getIngredient(image64));
    }

    /**
     * 菜品识别
     *
     * @param response
     * @param file
     * @return
     * @throws IOException
     */
    @PostMapping("/getDish")
    Result<ImageRecognitionRespVo> getDish(@RequestBody String image64) throws IOException {
        return Result.success(imageRecognitionService.getDish(image64));
    }

    /**
     * 红酒识别
     *
     * @param response
     * @param file
     * @return
     * @throws IOException
     */
    @PostMapping("/getRedWine")
    Result<ImageRecognitionRespVo> getRedWine(@RequestBody String image64) throws IOException {
        return Result.success(imageRecognitionService.getRedWine(image64));
    }

    /**
     * 货币识别
     *
     * @param response
     * @param file
     * @return
     * @throws IOException
     */
    @PostMapping("/getCurrency")
    Result<ImageRecognitionRespVo> getCurrency(@RequestBody String image64) throws IOException {
        return Result.success(imageRecognitionService.getCurrency(image64));
    }

    /**
     * 地标识别
     *
     * @param response
     * @param file
     * @return
     * @throws IOException
     */
    @PostMapping("/getLandmark")
    Result<ImageRecognitionRespVo> getLandmark(@RequestBody String image64) throws IOException {
        return Result.success(imageRecognitionService.getLandmark(image64));
    }

    /**
     * 百度百科
     *
     * @param msg
     * @return
     */
    @PostMapping("/getMsg")
    Result<JSONObject> getMsg(String msg) {
        return Result.success(imageRecognitionService.getMsg(msg));
    }

    /**
     * 查看图片
     *
     * @param fileName
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    @RequestMapping(value = "/images/baidu/{fileName:.+}", method = RequestMethod.GET)
    public void doPost(@PathVariable String fileName, HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        //读取本地图片输入流
        System.out.println(fileName);
        FileInputStream inputStream = new FileInputStream(UploadUtil.getRootPath() + "/images/baidu/" + fileName);
        int i = inputStream.available();
        //byte数组用于存放图片字节数据
        byte[] buff = new byte[i];
        inputStream.read(buff);
        //记得关闭输入流
        inputStream.close();
        //设置发送到客户端的响应内容类型
        response.setContentType("image/*");
        OutputStream out = response.getOutputStream();
        out.write(buff);
        //关闭响应输出流
        out.close();
    }

    /**
     * 测试
     *
     * @return
     */
    @PostMapping("/getTest1")
    Result<JSONObject> test1(@RequestBody String image64) {


        String json = readJsonFile(initApps);
        JSONObject body = JSON.parseObject(json);
        JSONObject jsonArray = body.getJSONObject("data");
        return Result.success(jsonArray);
    }
    @PostMapping("/getTest2")
    Result<JSONObject> test2(String name) {
        String json = readJsonFile(initApps);
        JSONObject body = JSON.parseObject(json);
        JSONObject jsonArray = body.getJSONObject("data");
        return Result.success(jsonArray);
    }

    public String readJsonFile(String filePath) {
        String jsonStr = null;
        try {
            InputStream inputStream = this.getClass().getResourceAsStream(filePath);
            InputStreamReader reader = new InputStreamReader(inputStream, StandardCharsets.UTF_8);
            int ch = 0;
            StringBuilder sb = new StringBuilder();
            while ((ch = reader.read()) != -1) {
                sb.append((char) ch);
            }
            reader.close();
            jsonStr = sb.toString();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return jsonStr;
    }
}
